import { createRouter, createWebHistory } from 'vue-router'

import routes from './routes'

import {useAuthStore} from "@/stores/auth"

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: routes,
})


router.beforeEach((to,from)=>{

  if(to?.meta?.requiredAuth){
    let AuthStore = useAuthStore();
    if(AuthStore.isLogin){
      return true;
    }else{
      // let route = router.currentRoute.value; // router.currentRoute当前路由信息 ref对象 => from
      router.push({path:"/login",query:{ReturnUrl:to.fullPath}})
    }

  }else{
    return true;
  }
})

export default router
